/*
Copyright (C) Ariff Ehsan, Abdul Hadi Fikri

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*  Author : Abdul Hadi Fikri
 *  Date   : June 18, 2009
 */


#include <system.h>
#include <leds/leds.h>
#include <button/button0.h>
#include <button/button1.h>

uint8_t startLed = 0x80;
uint8_t led0;
uint8_t led1;

uint8_t button0State;
uint8_t button1State;

uint8_t started;

timer_t timer_pattern1;
timer_t timer_pattern2;

void LED_PATTERN1(void);
void LED_PATTERN2(void);

void APP_booted(){

	button0State = 0;
	button1State = 1;

	BUTTON0_init();
	BUTTON1_init();	
	LEDS_init();
	
	TIMER_reg(&timer_pattern1,LED_PATTERN1);
	TIMER_reg(&timer_pattern2,LED_PATTERN2);		
}

void LED_PATTERN1(void){
	led0 ^= 0x07;
	LEDS_set(startLed|led0|led1);
}

void LED_PATTERN2(void){
	led1 ^= 0x38;
	LEDS_set(startLed|led0|led1);
}

void BUTTON0_pushed(void){
	if(!button0State){
		started =1;
		button0State = 1;
		LEDS_on(PINC7);
		return;
	}
	if(button0State&0x01){
	    TIMER_stop(&timer_pattern2);
		TIMER_stop(&timer_pattern1);
		
		LEDS_set(0);
		
		started =0;
		button0State=0;
		button1State=1;
		return;
	}
}
void BUTTON1_pushed(void){
	if(!started){
		return;
	}

	if(button1State==1){
		TIMER_stop(&timer_pattern2);
		led1=0;
	
		TIMER_startPeriodic(&timer_pattern1,100);
		button1State = 2;
		return;
	}
	if(button1State==2){
		TIMER_stop(&timer_pattern1);
	    led0=0;
		
		TIMER_startPeriodic(&timer_pattern2,300);
		button1State=3;
		return;
	}
	if(button1State==3){
		TIMER_startPeriodic(&timer_pattern1,100);
		TIMER_startPeriodic(&timer_pattern2,300);
		button1State = 1;
		return;
	}
}
void BUTTON0_released(){}
void BUTTON1_released(){}